package com.csaba.connector.dummy;

import java.util.logging.Logger;

import com.csaba.connector.DummyServer;
import com.csaba.connector.I18NServiceException;
import com.csaba.connector.ServiceException;
import com.csaba.connector.dummy.model.DummyBank;
import com.csaba.connector.model.Customer;
import com.csaba.connector.model.Session;
import com.csaba.connector.service.AbstractLoginService;
import com.csaba.util.LoggerHelper;

/**
 * @author Gabe
 *
 */
public class DummyLoginService extends AbstractLoginService
{
	private final static Logger logger = LoggerHelper.getDefaultLogger();
	private static long sessionCounter = 0;

	private Session createSession()
	{
		logger.info("Dummy: session being created.");
		Codes.waitABit();
		final Session session = new Session();
		session.setBank(DummyBank.getInstance());
		session.setSessionId("SSID" + ( sessionCounter++ ));
		session.setDummyInterval(10);
		return session;
	}

	@Override
	public void execute( Session session ) throws ServiceException
	{
		logger.info("Dummy: authenticated invoked.");

		final Customer dummy = new Customer();
		dummy.setBank(DummyBank.getInstance());

		if ( customer.getLoginId().equals("local") && customer.getPassword().equals("password") )
		{
			dummy.setId(Codes.LOCAL_CUSTOMER);
			dummy.setName("John Local");
		}
		else if ( customer.getLoginId().equals("foreign") && customer.getPassword().equals("password") )
		{
			dummy.setId(Codes.FOREIGN_CUSTOMER);
			dummy.setName("John Foreign");
		}
		else if ( customer.getLoginId().equals("error1") && customer.getPassword().equals("password") )
		{
			dummy.setId(Codes.ERROR_CUSTOMER);
			dummy.setName("Account Error");
		}
		else
			throw new I18NServiceException(DummyBank.getInstance(), "msg.failedLogin");

		if ( session == null || session.getCustomer() != null )
			session = createSession();

		dummy.setLoginId(customer.getLoginId());
		dummy.setPassword(customer.getPassword());
		dummy.setRemoteProperty("dummy.field", "dummy.value");
		dummy.setRemoteProperty("resident", "true");
		dummy.setRemoteProperty("status", "active");
		dummy.setRemoteProperty("activation.date", "2007-01-23 12:56:12");

		session.setCustomer(dummy);
		customer = dummy;
		this.session = session;
		DummyServer.getInstance().addSession(session);

		customer.addPermission(ALL_PERMISSION);

		Codes.waitABit();
	}

}
